package com.shujia.control;

import com.shujia.pojo.RedisList;
import com.shujia.pojo.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
public class RedisTestController {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/redisTest")
    public String testRedis() {
        // 设置值到redis
        // 用于操作redis中String类型数据
        redisTemplate.opsForValue().set("name","lucy");
        //从redis获取值
        String name = (String)redisTemplate.opsForValue().get("name");
//        redisTemplate.delete("hyper1");
        return name;
    }

    @RequestMapping(value = "/redis/set",method = RequestMethod.POST)
    public String redisSet(@RequestBody Set set) {
        // 设置值到redis
        // 用于操作redis中String类型数据
        redisTemplate.opsForValue().set(set.getKey(),set.getValue());
        return "创建成功...";
    }

    @RequestMapping(value = "/redis/list",method = RequestMethod.POST)
    public String redisSet(@RequestBody RedisList redisList) {
        // 设置值到redis
        // 用于操作redis中String类型数据
        for (String value : redisList.getValues()) {
            redisTemplate.opsForList().leftPush(redisList.getKey(),value);
        }
        return "创建成功...";
    }

}